<?php
$this->breadcrumbs=array(
//'Jump Order Home'=>array('/JumpOrder'),
	'Menu Builder',
);
$cs=Yii::app()->clientScript;
$ajaxAction=$this->createUrl('menuBuilder/ajaxPrice');
$script=<<<EOF
var refreshPrice=function(lineId){
var clientData={
	coffee_shopId: $('#coffee_shopId_'+lineId).val(),
	coffee_product: $('#coffee_product_'+lineId).val(),
	coffee_size: $('#coffee_size_'+lineId).val(),
	coffee_milk: $('#coffee_milk_'+lineId).val()
};
var params={};
$("#coffee_save_"+lineId).attr("disabled","disabled");
$('#menu_price_'+lineId).text("Loading price...");
$.ajax({
	dataType: 'json',
	error: function(XMLHttpRequest, textStatus, errorThrown){
		$('#menu_price_'+lineId).text("Error "+textStatus);
		window.location.href=window.location.href;
		},
	success: function (data, textStatus, XMLHttpRequest){
		
		if(data&&data.id){
		$('#coffee_prod_id_'+lineId).val(data.id);
		
			$('#menu_price_'+lineId).text('$'+(data.price/100).toFixed(2));
			$("#coffee_save_"+lineId).attr("disabled",null);
		}else{
			$('#menu_price_'+lineId).text("N/A");
		}
	},
	url: '$ajaxAction',
	data: clientData
}
);

};

EOF;

$cs->registerScript('menuBuilder_price',$script);

?>
<h1>Menu Builder</h1>
<div class="rc_main"><?php if(Yii::app()->user->hasFlash('jo_menuBuilder')){ ?>

<div class="flash-success"><?php echo Yii::app()->user->getFlash('jo_menuBuilder'); ?>
</div>
<?php } ?>
<h4>
<p>You've got a coffee you order most days of the week? Yes. Then simply
fill in the table below so it matches your regular brew and click
<STRONG><em>'save'</em></STRONG>.</p>
<p>You'll then be sent an email with your order details, and most
importantly, <STRONG><em>the number you call to order</em></STRONG>.</p>
<p>Call. Don't send us an SMS or expect us to answer. There's magic
going on here! You'll know it's done when you get your confirmation SMS
a minute or so later.</p>
<p></p>
<p></p>
<p></p>
</h4>
<div class="form">
<table cellpadding="0" cellspacing="0">
	<thead>
		<tr>
			<th>No.</th>
			<th>Cafe</th>
			<th>Coffee Type</th>
			<th>Size</th>
			<th>Milk</th>
			<!--<th>Size</th>
			<th>Milk</th>
			-->
			<th>Sugar</th>
			<th>Type</th>
			<th>Price (inc GST)</th>
			<th></th>
		</tr>
	</thead>

	<?php
	$line=0;

	foreach( $orderLines as $orderLine){
		$line++;
		$script=<<<EOF
(function($){
	$('#coffee_shopId_$line').change(function() {
  		refreshPrice($line);
	});
	$('#coffee_product_$line').change(function() {
  		refreshPrice($line);
	});
	$('#coffee_size_$line').change(function() {
  		refreshPrice($line);
	});
	$('#coffee_milk_$line').change(function() {
  		refreshPrice($line);
	});
	$("#coffee_save_"+$line).attr("disabled","disabled");
	refreshPrice($line);
	}
)($);
EOF;

		$cs->registerScript('menuBuilder_line_'.$line,$script);


		$form=$this->beginWidget('CActiveForm', array(
	'id'=>'coffee-order-menu-form',
	'enableAjaxValidation'=>false,
		));
		?>
	<tr>
		<td><?php 
		echo $line;
		?></td>
		<td><?php 
		echo CHtml::dropDownList('coffee_shopId',$orderLine->coffeeProduct?$orderLine->coffeeProduct->shop->id:0,
		$availableCoffeeShops,
		array('id'=>'coffee_shopId_'.$line,'style'=>'width:120px;')
		);
		?></td>
		<td><?php 
		echo CHtml::dropDownList('coffee_product',$orderLine->coffeeProduct?$orderLine->coffeeProduct->coffeeType:0,
		CoffeeProduct::coffee_types(),
		array('id'=>'coffee_product_'.$line,'style'=>'width:90px;')
		);
		?></td>
		<td><?php
		echo CHtml::dropDownList('coffee_size',$orderLine->coffeeProduct?$orderLine->coffeeProduct->size:0,
		CoffeeProduct::coffee_sizes(),
		array('id'=>'coffee_size_'.$line)
		);
		?></td>
		<td><?php
		echo CHtml::dropDownList('coffee_milk',$orderLine->coffeeProduct?$orderLine->coffeeProduct->milk:0,
		CoffeeProduct::coffee_milks(),
		array('id'=>'coffee_milk_'.$line)
		);
		?></td>
		<td><?php 
		echo $form->dropDownList($orderLine,'sugarNum',
		CoffeeProduct::coffee_sugar_nums()
		);
		?></td>
		<td><?php
		echo $form->dropDownList($orderLine,'sugarType',
		CoffeeProduct::coffee_sugar_types()
		);
		?></td>
		<td>
		<div id="menu_price_<?php echo $line;?>"></div>
		</td>
		<td><?php 
		echo $form->hiddenField($orderLine,'coffeeProductId',array('id'=>'coffee_prod_id_'.$line));
		echo $form->hiddenField($orderLine,'lineId');

		echo CHtml::submitButton('Save',array('id'=>'coffee_save_'.$line,'name'=>'action','class'=>'confirmAction'));
		echo CHtml::submitButton('Clear',array('id'=>'coffee_clear_'.$line,'name'=>'action','class'=>'confirmAction')); ?></td>
	</tr>

	<?php
	$this->endWidget();

	}
	?>

</table>

	<?php  ?></div>

<h4>
<p></p>
<p><STRONG>QUESTIONS & COMMENTS</STRONG></p>
<p>If you don't see what you want here, or you have a great idea for Jump Order, please drop Mark Sefton an email at msefton@jumporder.com or call him on 0417544616. <STRONG><em>There might even be a few free coffees in it!</em></STRONG></p>
<p></p>
<p></p>
</h4>

<!-- form --></div>
